package com.longrise.android.byjk.plugins.course.coursedetail.videoexercises;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.webkit.WebView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.CourseTrainDirBean;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.params.VideoParams;
import com.longrise.android.byjk.utils.GetWebUrlHelper;
import com.longrise.common.base.web.BaseWebActivity;
import com.longrise.common.base.web.BaseWebBridge;
import com.longrise.common.base.web.BaseWebClient;


/**
 * Created by callxkj on 2017/10/26.
 * 课后练习题
 */

public class VideoExercisesActvity extends BaseWebActivity {

    public static final String COURSEBEAN = "CourseBean";

    private WebView mWv;
    private VideoParams mVideoParams;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.web_comment;
    }

    @Override
    public WebView getWebView() {
        return mWv;
    }

    @Override
    public void init() {
        mWv = (WebView) findViewById(R.id.comment_wv);
        getIntentData();
    }

    private void getIntentData() {
        Intent intent = getIntent();
        mVideoParams = intent.getParcelableExtra(COURSEBEAN);
    }


    @Override
    public void initData() {
        GetWebUrlHelper.getWebUrl(GetWebUrlHelper.KHLX, new GetWebUrlHelper.OnGetWebUrlResultListener() {
            @Override
            public void onSuccess(String url) {
                loadUrl(assemble(url));
            }

            @Override
            public void onError(String errormsg) {
                showToast(errormsg);
            }
        });
    }

    @Override
    public BaseWebBridge getwebBridge() {
        return new VideoExercisesBridge(this, mRxmanager);
    }

    @Override
    public BaseWebClient getWebClient() {
        return new BaseWebClient(this);
    }


    /**
     * 重写onKeyDown 返回调用web的返回
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        try {
            if (keyCode == KeyEvent.KEYCODE_BACK && mWv.canGoBack()) {
                goH5back();
                return true;
            }
            if (event == null) {
                finish();
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 调用web写的返回方法
     */
    @Override
    public void onToolbarBackClick() {
        goH5back();
    }

    /**
     * 拼接url
     */
    private String assemble(String url) {
        if (mVideoParams != null) {
            String studentno = mVideoParams.mStudentNo;
            String cwid = mVideoParams.mCwid;
            String pid = mVideoParams.mCourseId;
            url = url + "&stuno=" + studentno + "&cwid=" + cwid + "&pid=" + pid;
        }
        return url;
    }
}
